#include "cartoonreader.h"

#include <QtCore/QDir>
#include <QtCore/QUrl>

#include <QtGui/QWidget>
#include <QtGui/QVBoxLayout>
#include <QtGui/QFrame>
#include <QtGui/QDesktopServices>

#include <QtWebKit/QWebView>
#include <QtWebKit/QWebPage>
#include <QtWebKit/QWebFrame>
#include <QtWebKit/QWebSettings>
#include <QtWebKit/QWebSecurityOrigin>

#include <QtCore/QDebug>

#include <QtNetwork/QNetworkProxy>


namespace CartoonReader {

    CartoonReader::CartoonReader(QWidget *parent)
        : QMainWindow(parent)
    {
        m_wrapper = new WindowWrapper(this);
        m_platformResolver = new PlatformResolver(this);
        setupUI();
    }

    CartoonReader::~CartoonReader()
    {

    }

    void CartoonReader::setupUI()
    {
        /**
          * Create the central widget
          * and set it.
          */
        QFrame* cW = new QFrame(this);
        setCentralWidget(cW);

        /**
          * Set the layout to central widget.
          */
        QVBoxLayout* layout = new QVBoxLayout(cW);
        cW->setLayout(layout);
        layout->setMargin(0);
        layout->setSpacing(0);

        /**
          * Let's create the web view which
          * will be used to display our page
          * with which we will be communicating with.
          */
        m_webView = createWebView();
        /** Add it to layout */
        layout->addWidget(m_webView);

        m_webView->show();
    }

    QWebView* CartoonReader::createWebView()
    {
        QWebSettings* defaultSettings = QWebSettings::globalSettings();
        defaultSettings->setAttribute(QWebSettings::JavascriptEnabled, true);
        defaultSettings->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
#ifdef DEBUG
        defaultSettings->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
#endif
        #if QT_VERSION >= 0x040600
        defaultSettings->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);
        #endif
        QString dbDir = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
        QWebSettings::setOfflineStoragePath(dbDir);
        QWebSettings::setOfflineStorageDefaultQuota(500000);

        QWebView* webView = new QWebView(this);
#if !defined(DEBUG)
        webView->setContextMenuPolicy(Qt::NoContextMenu);
#endif
        connect(webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
                this, SLOT(addJavascriptObject()));

#if !defined(Q_WS_MAEMO_5) && !defined(Q_OS_SYMBIAN)
        //setting the system default proxy that would be used to reach google.com
        QList<QNetworkProxy> list = QNetworkProxyFactory::systemProxyForQuery ( QNetworkProxyQuery ( QUrl("http://www.google.com/") ) );
        if( list.count() >= 1)
            QNetworkProxy::setApplicationProxy ( list.at(0) );
#endif
        webView->load(QUrl("qrc:/html/cartoon-view.html?init=true"));
        return webView;
    }

    void CartoonReader::addJavascriptObject()
    {
        if(this->m_webView) {
            qDebug() << "CartoonReader::addJavascriptObject: Adding WindowWrapper to webview.";
            this->m_webView->page()
                           ->mainFrame()
                           ->addToJavaScriptWindowObject("appWindow",
                                                         this->m_wrapper);
            qDebug() << "CartoonReader::addJavascriptObject: Adding PlatformResolver to webview.";
            this->m_webView->page()
                           ->mainFrame()
                           ->addToJavaScriptWindowObject("platformResolver",
                                                         this->m_platformResolver);
        }
    }
}
